import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ConfigModule, ConfigService } from "@nestjs/config";
import databaseConfig from "./config/database.config";
import { UserModule } from "./user/user.module";
import { DatabaseModule } from "./database/database.module";

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [databaseConfig],
      isGlobal: true,
      envFilePath: [".env", ".env.development", ".env.production"],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService): any => ({
        type: configService.get("database.type"),
        host: configService.get("database.host"),
        port: configService.get("database.port"),
        username: configService.get("database.username"),
        password: configService.get("database.password"),
        database: configService.get("database.database"),
        entities: [
          __dirname + "src/**/*.entity{.ts,.js}",
          __dirname + "/entity/*.{ts,js}"
        ],
        synchronize: configService.get("database.synchronize"),
        logging: configService.get("database.logging"),
        poolSize: 10,
        connectorPackage: "mysql2",
        extra: {
          authPlugin: "sha256_password",
        },
      }),
    }),
    DatabaseModule,
    UserModule,
  ],
})
export class AppModule {}
